package com.stars.easyms.logger.enums;

import com.stars.easyms.logger.EasyMsLoggerHandler;
import com.stars.easyms.logger.support.log4j12.handler.EasyMsLog4j12LoggerHandler;
import com.stars.easyms.logger.support.logback.handler.EasyMsLogbackLoggerHandler;

import java.util.function.Supplier;

/**
 * <p>enumName: LogFrameworkType</p>
 * <p>description: 日志框架类型</p>
 *
 * @author guoguifang
 * @date 2019-11-19 13:49
 * @since 1.4.0
 */
public enum LogFrameworkType {

    /**
     * logback
     */
    LOGBACK("logback",
            "ch.qos.logback.core.Appender",
            com.stars.easyms.logger.support.logback.redefine.EasyMsStaticLoggerBinder.class,
            EasyMsLogbackLoggerHandler::getInstance,
            1),

    /**
     * log4j
     */
    LOG4J("log4j",
            "org.apache.log4j.Appender",
            com.stars.easyms.logger.support.log4j12.redefine.EasyMsStaticLoggerBinder.class,
            EasyMsLog4j12LoggerHandler::new,
            2);

    private String code;

    private String guide;

    private Class<?> redefineClass;

    private Supplier<EasyMsLoggerHandler> easyMsLoggerHandlerSupplier;

    private EasyMsLoggerHandler easyMsLoggerHandler;

    private int priority;

    LogFrameworkType(String code, String guide, Class<?> redefineClass, Supplier<EasyMsLoggerHandler> easyMsLoggerHandlerSupplier, int priority) {
        this.code = code;
        this.guide = guide;
        this.redefineClass = redefineClass;
        this.easyMsLoggerHandlerSupplier = easyMsLoggerHandlerSupplier;
        this.priority = priority;
    }

    public String getCode() {
        return code;
    }

    public String getGuide() {
        return guide;
    }

    public Class<?> getRedefineClass() {
        return redefineClass;
    }

    @SuppressWarnings("unchecked")
    public synchronized <E> EasyMsLoggerHandler<E> getEasyMsLoggerHandler() {
        if (easyMsLoggerHandler == null) {
            easyMsLoggerHandler = easyMsLoggerHandlerSupplier.get();
        }
        return easyMsLoggerHandler;
    }

    public int getPriority() {
        return priority;
    }
}
